package org.summer.lawyer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.satoken.utils.LoginHelper;
import org.summer.lawyer.domain.LawyerInfo;
import org.summer.lawyer.domain.bo.lawyerinfo.LawyerInfoBo;
import org.summer.lawyer.domain.vo.lawyerinfo.LawyerInfoVo;
import org.summer.lawyer.mapper.LawyerInfoMapper;
import org.summer.lawyer.service.ILawyerInfoService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class LawyerInfoServiceImpl extends ServiceImpl<LawyerInfoMapper, LawyerInfo> implements ILawyerInfoService {


    // private final ILawFirmService lawFirmService;

    @Override
    public TableDataInfo<LawyerInfoVo> selectListByAdvice() {
        LambdaQueryWrapper<LawyerInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(LawyerInfo::getAdviceStatus, "1")
                .orderByAsc(LawyerInfo::getId);
        return TableDataInfo.build(baseMapper.selectVoList(queryWrapper));
    }

    @Override
    public LawyerInfoVo selectLawyerVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public LawyerInfoVo selectLawyerVoByWxUserId(Long wxUserId) {
        LambdaQueryWrapper<LawyerInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(LawyerInfo::getWxUserId, wxUserId);
        return baseMapper.selectVoOne(queryWrapper, false);
    }

    /**
     * 查询律师信息
     *
     * @param id 主键
     * @return 律师信息
     */
    @Override
    public LawyerInfoVo selectLawyerInfoVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询律师信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 律师信息分页列表
     */
    @Override
    public TableDataInfo<LawyerInfoVo> selectPageList(LawyerInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LawyerInfo> lqw = buildQueryWrapper(bo);
        Page<LawyerInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的律师信息列表
     *
     * @param bo 查询条件
     * @return 律师信息列表
     */
    @Override
    public List<LawyerInfoVo> selectLawyerInfoVoList(LawyerInfoBo bo) {
        LambdaQueryWrapper<LawyerInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LawyerInfo> buildQueryWrapper(LawyerInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LawyerInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWxUserId() != null, LawyerInfo::getWxUserId, bo.getWxUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getAvatar()), LawyerInfo::getAvatar, bo.getAvatar());
        lqw.like(StrUtil.isNotBlank(bo.getLawyerName()), LawyerInfo::getLawyerName, bo.getLawyerName());
        lqw.eq(bo.getLawFirmId() != null, LawyerInfo::getLawFirmId, bo.getLawFirmId());
        lqw.eq(StrUtil.isNotBlank(bo.getGroupType()), LawyerInfo::getGroupType, bo.getGroupType());
        lqw.eq(bo.getPractice() != null, LawyerInfo::getPractice, bo.getPractice());
        lqw.eq(bo.getPracticeStartTime() != null, LawyerInfo::getPracticeStartTime, bo.getPracticeStartTime());
        lqw.eq(bo.getConsultation() != null, LawyerInfo::getConsultation, bo.getConsultation());
        lqw.eq(StrUtil.isNotBlank(bo.getSpecial()), LawyerInfo::getSpecial, bo.getSpecial());
        lqw.eq(bo.getRating() != null, LawyerInfo::getRating, bo.getRating());
        lqw.eq(StrUtil.isNotBlank(bo.getFeedback()), LawyerInfo::getFeedback, bo.getFeedback());
        lqw.eq(StrUtil.isNotBlank(bo.getProvince()), LawyerInfo::getProvince, bo.getProvince());
        lqw.eq(StrUtil.isNotBlank(bo.getCity()), LawyerInfo::getCity, bo.getCity());
        lqw.eq(StrUtil.isNotBlank(bo.getDistrict()), LawyerInfo::getDistrict, bo.getDistrict());
        lqw.eq(StrUtil.isNotBlank(bo.getOfficeAddress()), LawyerInfo::getOfficeAddress, bo.getOfficeAddress());
        lqw.eq(StrUtil.isNotBlank(bo.getAdviceStatus()), LawyerInfo::getAdviceStatus, bo.getAdviceStatus());
        lqw.eq(bo.getFollowers() != null, LawyerInfo::getFollowers, bo.getFollowers());
        lqw.eq(StrUtil.isNotBlank(bo.getLawyerDesc()), LawyerInfo::getLawyerDesc, bo.getLawyerDesc());
        return lqw;
    }

    /**
     * 新增律师信息
     *
     * @param bo 律师信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(LawyerInfoBo bo) {
        LawyerInfo add = MapstructUtils.convert(bo, LawyerInfo.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtil.date());
        add.setUpdateTime(DateUtil.date());
        if (LoginHelper.getLoginUser() != null) {
            add.setCreateBy(LoginHelper.getUserId());
            add.setUpdateBy(LoginHelper.getUserId());
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改律师信息
     *
     * @param bo 律师信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(LawyerInfoBo bo) {
        LawyerInfo update = MapstructUtils.convert(bo, LawyerInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LawyerInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除律师信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void saveLawyer(LawyerInfoBo bo) {
        if (bo.getId() == null) {
            insertByBo(bo);
        } else {
            updateByBo(bo);
        }
    }

    @Override
    public void updateLawyerArchive(LawyerInfoBo bo) {
        List<LawyerInfoVo> list = selectLawyerInfoVoList(bo);
        if(CollUtil.isNotEmpty(list)){
            LawyerInfoVo vo = list.getFirst();
            LawyerInfo lawyerInfo = new LawyerInfo();
            lawyerInfo.setId(vo.getId());
            lawyerInfo.setLawyerDesc(bo.getLawyerDesc());
            lawyerInfo.setSpecial(bo.getSpecial());
            lawyerInfo.setOfficeAddress(bo.getOfficeAddress());
            LambdaUpdateWrapper<LawyerInfo> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.eq(LawyerInfo::getId, vo.getId());
            baseMapper.update(lawyerInfo, updateWrapper);
        }
    }
}
